package com.jjb.www.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeRecyclerView;
import com.hjq.shape.view.ShapeEditText;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.bean.RightBean;
import com.jjb.www.http.api.PlanListApi;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.ui.adapter.SelectLeftAdapter;
import com.jjb.www.ui.adapter.SelectRightAdapter;
import com.jjb.www.ui.fragment.PreListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 选择岗位
 */
public class SelectPostActivity extends AppActivity implements TextWatcher {
    private ShapeEditText mEtSearch;
    private ShapeRecyclerView mRvLeftName;
    private ShapeRecyclerView mRvRightName;
    private SelectLeftAdapter mLeftAdapter;
    private SelectRightAdapter mRightAdapter;

    private String leftName = "";
    private String rightName = "";
    //home 首页进来，pre 岗前报名列表
    private String fromType = "";
    private Integer leftPersion = 0;
    private Integer rightPersion = 0;
    private  Integer pid = 1;
    private List<String> leftList = new ArrayList<>();
    private List<RightBean> rightList = new ArrayList<>();
    private List<RightBean> seachRightList = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_post;
    }

    @Override
    protected void initView() {
        mEtSearch = findViewById(R.id.et_search);
        mRvLeftName = findViewById(R.id.rv_left_name);
        mRvRightName = findViewById(R.id.rv_right_name);

        mEtSearch.addTextChangedListener(this);
        mRvLeftName.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvRightName.setLayoutManager(new LinearLayoutManager(getContext()));

        mLeftAdapter = new SelectLeftAdapter(getContext());

        mLeftAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                leftPersion = position;
                mLeftAdapter.setItemSel(position);

                if ("专业人员".equals(mLeftAdapter.getItem(position))){
                    pid = 1;
                }else if ("检测人员".equals(mLeftAdapter.getItem(position))){
                    pid = 44;
                }else if ("特种作业人员".equals(mLeftAdapter.getItem(position))){
                    pid = 89;
                }else if ("安管人员".equals(mLeftAdapter.getItem(position))){
                    pid = 114;
                }else if ("监理人员".equals(mLeftAdapter.getItem(position))){
                    pid = 143;
                }
                getPlanList(pid);
//                mRightAdapter.setData(rightList);
//                if (mLeftAdapter.getItem(position).equals(leftName)){
//                    mRightAdapter.setItemSel(rightPersion);
//                }else {
//                    mRightAdapter.setItemSel(0);
//                }
            }
        });

        mRightAdapter = new SelectRightAdapter(getContext());
        mRightAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                rightPersion = position;
                if ("home".equals(fromType)){
                    Intent intent = new Intent();
                    intent.putExtra("name",mRightAdapter.getItem(position).getName());
                    intent.putExtra("sentry_id",mRightAdapter.getItem(position).getId());
                    intent.setClass(getContext(),DataActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }

                if ("".equals(mEtSearch.getText().toString())){
                    setResult(RESULT_OK, new Intent()
                            .putExtra("seachName","" )
                            .putExtra("leftName",mLeftAdapter.getItem(leftPersion) )
                            .putExtra("rightName", mRightAdapter.getItem(position).getName()));
                }else {
                    setResult(RESULT_OK, new Intent()
                            .putExtra("seachName", mEtSearch.getText().toString())
                            .putExtra("leftName",mLeftAdapter.getItem(leftPersion) )
                            .putExtra("rightName", mRightAdapter.getItem(position).getName()));
                }

                finish();
            }
        });

        mRvRightName.setAdapter(mRightAdapter);
        mRvLeftName.setAdapter(mLeftAdapter);
    }

    @Override
    protected void initData() {
        leftName = getString("leftName");
        rightName = getString("rightName");
        fromType = getString("fromType");
//        seachName = getString("seachName");
//        mEtSearch.setText(seachName);

        leftList.add("专业人员");
        leftList.add("特种作业人员");
        leftList.add("检测人员");
        leftList.add("监理人员");
        leftList.add("安管人员");

        mLeftAdapter.setData(leftList);


     if ("".equals(leftName)){
         mLeftAdapter.setItemSel(0);
         mRightAdapter.setItemSel(0);
         getPlanList(pid);
     }else {
         for (int i = 0; i < leftList.size(); i++) {
                 if (leftList.get(i).equals(leftName)){
                     leftPersion = i;
                     mLeftAdapter.setItemSel(i);
                 }
         }
         /**
          * 岗位id
          * 1	专业人员
          * 44	检测人员
          * 89	特种作业人员
          * 114	安管人员
          * 143	监理人员
          */

         if ("专业人员".equals(leftName)){
             pid = 1;
         }else if ("检测人员".equals(leftName)){
             pid = 44;
         }else if ("特种作业人员".equals(leftName)){
             pid = 89;
         }else if ("安管人员".equals(leftName)){
             pid = 114;
         }else if ("监理人员".equals(leftName)){
             pid = 143;
         }
         getPlanList(pid);
     }

    }

    @Override
    public void onLeftClick(View view) {
        if ("".equals(mEtSearch.getText().toString())){
            setResult(RESULT_OK, new Intent()
                    .putExtra("seachName","" )
                    .putExtra("leftName",leftName )
                    .putExtra("rightName", rightName));
        }else {
            setResult(RESULT_OK, new Intent()
                    .putExtra("seachName", mEtSearch.getText().toString())
                    .putExtra("leftName",leftName )
                    .putExtra("rightName", rightName));
        }
        finish();

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        seachRightList.clear();
        String content = s.toString();
        if (!TextUtils.isEmpty(content)){
            for (RightBean name:rightList) {
                if (name.getName().contains(content)){
                    seachRightList.add(name);
                }
            }
            mRightAdapter.setData(seachRightList);
            mRightAdapter.setItemSel(-1);
        } else {
            mRightAdapter.setData(rightList);
            if (!"".equals(rightName)){
                for (int i = 0; i < rightList.size(); i++) {
                    if (rightList.get(i).equals(rightName)){
                        rightPersion = i;
                        mRightAdapter.setItemSel(i);
                    }
                }
            }


        }
    }

    /**
     * 根据专业获取岗位
     */
    private void getPlanList(Integer mPid) {
        EasyHttp.get(this)
                .api(new PlanListApi()
                        .setPid(mPid+""))
                .request(new HttpCallback<HttpList<PlanListApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpList<PlanListApi.Bean> data) {
                        rightList.clear();
                        if (!"home".equals(fromType)){
                            RightBean rightBean = new RightBean();
                            rightBean.setName("全部");
                            rightBean.setId(0);
                            rightList.add(rightBean);
                        }
                        Boolean isSelect = false;
                        for (PlanListApi.Bean bean:data.getData()) {
                            RightBean rightBean1 = new RightBean();
                            rightBean1.setName(bean.getNew_name());
                            rightBean1.setId(bean.getId());
                            rightList.add(rightBean1);
                        }
                        mRightAdapter.setData(rightList);

                        for (int i = 0; i < rightList.size(); i++) {
                            if (rightList.get(i).equals(rightName)){
                                isSelect = true;
                                rightPersion = i;
                                mRightAdapter.setItemSel(i);
                            }
                        }

                        if (!isSelect){
                            mRightAdapter.setItemSel(0);
                        }
                    }

                });
    }
}